understanding this PHP code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: understanding this PHP code

Post by jonnyfortis »

ok, this is what i dont understand with the code, in respect to what part of the code is calling the tables and what is being associated with other parts of the code


does that make sense?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: understanding this PHP code

Post by requinix »

Not your code. The query. Which is in your code yes but I do mean the query itself.

Code: Select all

$query2 = sprintf("
	SELECT DISTINCT
		stock.StockID, size.Size 
	FROM 
		beauProd AS prod 
		LEFT JOIN beauStock AS stock ON prod.ID = stock.ID 
		LEFT JOIN beauSizeList AS size ON stock.SizeID = size.SizeID
	WHERE
		prod.ID = '%s' AND stock.Stock > 0
	ORDER BY
		size.SizeID ASC", GetSQLValueString($var1_Recordset1, "int"));
That. Echo out $query2 and try running it yourself in MySQL (like with a console or phpMyAdmin).
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: understanding this PHP code

Post by Christopher »

jonnyfortis wrote:

Code: Select all

<option value="Select Size">Select Size</option>
      <br />
<b>Warning</b>:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>E:\Domains\b\beau.com\user\htdocs\SS13\product-description.php</b> on line <b>338</b><br />
    </select>
Not much help after the fact. I recommend wrapping the code after a query with error checking. The error message from the query will help tell you what is going wrong.

Code: Select all

                            $results2 = mysql_query($query2);
                            if (!mysql_errno($query2)) {
                                while($row2 = mysql_fetch_array($results2)){
                                        ?>
      <option value="<?php echo $row2['Size']; ?>"><?php echo $row2['Size']; ?></option>
      <?php
                                }
                            } else {
                                echo "DB ERROR: " . mysql_error($query2);
                            }
(#10850)
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: understanding this PHP code

Post by jonnyfortis »

$results2 = mysql_query($query2);
if (!mysql_errno($query2)) {
while($row2 = mysql_fetch_array($results2)){
?>
<option value="<?php echo $row2['Size']; ?>"><?php echo $row2['Size']; ?></option>
<?php
}
} else {
echo "DB ERROR: " . mysql_error($query2);
}
the results in the source code are

Code: Select all

      <option value="Select Size">Select Size</option>
      <br />
<b>Warning</b>:  mysql_errno(): supplied argument is not a valid MySQL-Link resource in <b>E:\Domains\b\beau.com\user\htdocs\SS13\product-description.php</b> on line <b>338</b><br />
<br />
<b>Warning</b>:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>E:\Domains\b\beau.com\user\htdocs\SS13\product-description.php</b> on line <b>339</b><br />
    </select>

line 338 and 339 of the code are

Code: Select all

if (!mysql_errno($query2)) {
                                while($row2 = mysql_fetch_array($results2)){
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: understanding this PHP code

Post by Christopher »

"mysql_errno(): supplied argument is not a valid MySQL-Link resource" means that you do not have a connection to the database.
(#10850)
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: understanding this PHP code

Post by jonnyfortis »

hmm? but i do have a connection for the rest of the page,

<?php require_once('../Connections/beau.php'); ?>


do i need to specify the connection again for the other query?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: understanding this PHP code

Post by Christopher »

It the connection for $query or $query2?
(#10850)
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: understanding this PHP code

Post by jonnyfortis »

It the connection for $query or $query2?
that shown connection is at the top of the page so thought that would be for all querys
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: understanding this PHP code

Post by Christopher »

Sorry $query2 is the SQL (calling it $sql would be clearer). I don't see anywhere in this code where you call mysql_connnect().
(#10850)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: understanding this PHP code

Post by requinix »

Christopher wrote:"mysql_errno(): supplied argument is not a valid MySQL-Link resource" means that you do not have a connection to the database.
It could also mean he didn't pass in the right variable.

Code: Select all

if (!mysql_errno($query2)) {
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: understanding this PHP code

Post by Christopher »

requinix wrote:It could also mean he didn't pass in the right variable.

Code: Select all

if (!mysql_errno($query2)) {
Definitely didn't! That variable is the string with the SQL statement! It would be great to have variables like $sql and $result.
(#10850)
Post Reply