[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

code using while

hello there,
I would like to know if its possible to do something like this, or what other procedure should I take?
while($re1 = mysql_fetch_assoc($qu1) || $Test !== FALSE) {
     //my code
}

Open in new window

I get a warning when using test instead of mysql_fetch_assoc
>Warning: mysql_fetch_assoc() expects parameter 1 to be resource
0
XK8ER
Asked:
XK8ER
1 Solution
 
Ray PaseurCommented:
The warning means that $qul is not a resource.  If is was returned from mysql_query() and it happens to be boolean FALSE, it means the query failed.  See the code snippet for ways to do some of the basics in PHP and MySQL.  See especially lines 57-67 where it shows how to test for query failures and show the error messages that can help you fix the query.
<?php // RAY_mysql_example.php
error_reporting(E_ALL);


// THE ABSOLUTE MINIMUM YOU MUST UNDERSTAND TO USE PHP AND MYSQL
// MAN PAGE: http://php.net/manual/en/ref.mysql.php
// MAN PAGE: http://php.net/manual/en/mysql.installation.php
// MAN PAGE: http://php.net/manual/en/function.mysql-connect.php
// MAN PAGE: http://php.net/manual/en/function.mysql-select-db.php
// MAN PAGE: http://php.net/manual/en/function.mysql-real-escape-string.php
// MAN PAGE: http://php.net/manual/en/function.mysql-query.php
// MAN PAGE: http://php.net/manual/en/function.mysql-errno.php
// MAN PAGE: http://php.net/manual/en/function.mysql-error.php
// MAN PAGE: http://php.net/manual/en/function.mysql-num-rows.php
// MAN PAGE: http://php.net/manual/en/function.mysql-fetch-assoc.php
// MAN PAGE: http://php.net/manual/en/function.mysql-fetch-array.php
// MAN PAGE: http://php.net/manual/en/function.mysql-insert-id.php



// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";


// OPEN A CONNECTION TO THE DATA BASE SERVER
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}
// IF THE SCRIPT GETS THIS FAR IT CAN DO QUERIES




// ESCAPE AN EXTERNAL DATA FIELD FOR USE IN MYSQL QUERIES
$safe_username = mysql_real_escape_string($_POST["username"]);




// CREATE AND SEND A SELECT QUERY AND TEST THE RESULTS
$sql = "SELECT id FROM my_table WHERE username='$safe_username'";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, SHOW THE ERROR
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}
// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESOURCE-ID IN $res SO WE CAN NOW USE $res IN OTHER MYSQL FUNCTIONS




// DETERMINE HOW MANY ROWS OF RESULTS WE GOT
$num = mysql_num_rows($res);
if (!$num)
{
    echo "<br/>QUERY FOUND NO DATA: ";
    echo "<br/>$sql <br/>";
}
else
{
    echo "<br/>QUERY FOUND $num ROWS OF DATA ";
    echo "<br/>$sql <br/>";
}




// ITERATE OVER THE RESULTS SET TO SHOW WHAT WE FOUND
while ($row = mysql_fetch_assoc($res))
{
    var_dump($row);
}




// A WAY OF DETERMINING HOW MANY ROWS WE HAVE IN A TABLE
$sql = "SELECT COUNT(*) FROM my_table";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}
// GET THE RESULTS SET ROW IN AN ARRAY WITH A NUMERIC INDEX - POSITION ZERO IS THE COUNT
$row = mysql_fetch_array($res, MYSQL_NUM);
$num = $row[0];
$fmt = number_format($num);
echo "<br/>THERE ARE $fmt ROWS IN THE TABLE";




// MAKING AN INSERT QUERY AND TESTING THE RESULTS
$sql = "INSERT INTO my_table (username) VALUES ('$safe_username')";
$res = mysql_query($sql);

// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>QUERY FAIL: ";
    echo "<br/>$sql <br/>";
    die($errmsg);
}
// GET THE AUTO_INCREMENT ID OF THE RECORD JUST INSERTED - PER THE DB CONNECTION
$id  = mysql_insert_id($db_connection);
echo "<br/>YOU JUST INSERTED A RECORD WITH AUTO_INCREMENT ID = $id";

Open in new window

0
 
XK8ERAuthor Commented:
can you please give me some simplified answers..
0
 
Hugh McCurdyCommented:
I can try to give you a more simplified answer but Ray's code is very good.  I think it would be better for me to explain what Ray did.  (Or at least what I think it does.)

The key point is that it is critical to check the value returned by mysql_query() to make sure it doesn't return an error.  I'll go through Ray's code, line by line starting at line 57.

57: This is the my sql query.  It returns either the resource or false upon error.
60: This if statement is true if the resource variable is false.  (There was an error)
61-66: These instructions are executed only if mysql_query returned false.
62: Get the error number and the description of the error from the system.
63: Notify the user of the error.
64: Tell the user more about the error.
65: Show the error number and description to the user and stop execution of the PHP script (program).

73: You reach this line only if the resource returned from mysql_query can be used.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
@hmccurdy: I think that is an excellent summary!

Overall, the biggest two problems faced by novice programmers are these:

1. I do not know what the data looks like.
2. I do not know how the instructions change the way the data looks.

The solution to both of these issues is the same.  Visualize the data.  Use var_dump() to print out the data after every instruction that changes the data.  You may think this is "overkill" but it is not.   I rarely write more that three or four lines of code without a data-visualization statement: echo, print_r() or var_dump().  This is at the center of test-driven development.  And since I write several hundred lines of working code a day, it means that I am always looking at the output of my scripts, and the changes to my scripts, no matter how small and insignificant these changes might seem.  It is not uncommon for me to test a 10-line script 20 times.

I learned this approach the hard way.  I took a programming assignment that required me to use a server without any error reporting.  If I had a parse error, the page went blank.  Nothing: no messages, no data, just silence.  As a result I found that I had to work through my programming a line at a time to avoid the blank screen of death.  In that environment I knew that if I made a mistake, it was a mistake in the last line of code I wrote.  And from that experience I developed an organic understanding of the value of test-driven development.

There is a little more of this philosophy in this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

Best regards, ~Ray
0
 
Hugh McCurdyCommented:
@Ray, thanks for the opinion on novice programmers.  I'm going to help with a C/C++ class in November.  Many of the students will be new to programming.

I very much agree with data visualization and frequent testing.  I use it frequently.

Hugh
0
 
Dave BaldwinFixer of ProblemsCommented:
Me too on "data visualization".  I often write little test programs to display results before incorporating the code into a larger program.  I don't like dealing with too many things at once if I don't have to.  And for PHP, http://www.php.net is your best friend.
0
 
Ray PaseurCommented:
Great minds think alike!  Thanks to all of you, my colleagues, ~Ray
0
 
Hugh McCurdyCommented:
How many points do we need for colleague?  (j/k)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now