0

My problem is the following: I want to display a single row from a sql database

Link is: /index.php?wh=1

code:

$wh_id=($_GET["wh"]);
$wh_qr=mysql_query("SELECT name FROM warehouse WHERE warehouseid = '$wh_id';");

on echo $wh_qr i get only Resource id #5, not the value of the "name" in the Database.

What do i wrong?

5
  • You need to do some type of loop, either a while or foreach to iterate through results found (if any exist). You can't just echo a query on its own. Show us your full code. Commented Dec 15, 2013 at 17:57
  • its only one result?, and if it where more than on, it has to select only on. cuz it should display in the end something like: "You are currently on: <?php echo $wh_qr ?>" Commented Dec 15, 2013 at 17:59
  • I believe what I mentioned can be found in the answer(s) given below. Commented Dec 15, 2013 at 18:02
  • 3
    imgs.xkcd.com/comics/exploits_of_a_mom.png Commented Dec 15, 2013 at 18:11
  • That is the funniest SQL injection comic I've ever seen. Commented Dec 16, 2013 at 15:09

2 Answers 2

2

mysql_query returns a mysql result and not a string so you could echo, what you need to do is

$wh_id=($_GET["wh"]);
$wh_qr=mysql_query("SELECT name FROM warehouse WHERE warehouseid = '$wh_id';");
$row = mysql_fetch_assoc($wh_qr);
echo $row['name'];

Please do note that you should stop using mysql_* functions since they're deprecated, you also need to validate and sanitize whatever user input you get. start using PDO or Mysqli and start using prepared statements.

Sign up to request clarification or add additional context in comments.

10 Comments

It does not work... Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, resource given in C:\xampp\htdocs\index2.php on line 20
@user3101966 small typo, fixed. try it now.
That's because the answer contains mysql_ functions instead of mysqli_ @user3101966 which is "one" of the problems. I asked you to show us your full code earlier.
@Fred-ii- yeah sorry that's my bad, I fixed it. got used to writing mysqli instead of mysql.
THANKS! That works perfectly. What about MSQLI? Is it better? Does every Database support's this? My Database is old, i just renew the web-interface. Database is from 2005...
|
-1

You have to do the following. I also added in some error handling for you.

$link = mysqli_connect("<HOST>", "<USERNAME>", "<PASSWORD>", "<DATABASE_NAME");
$result = mysqli_query($link,"SELECT name FROM warehouse WHERE warehouseid = '$wh_id';");
if (!$result) {
    echo 'Could not run query: ' . mysqli_error();
    exit;
}
$row = mysqli_fetch_row($result);
$echo $row["name"];

Then output the $row and you should get what you're looking for. $row will be an array of strings, one per column that you expected from your query.

Also, all of the functions you are using are deprecated. You should be using the mysqli* functions.

12 Comments

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\index2.php on line 19 Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\index2.php on line 20
If you can provide your database connection variable name, that simply needs to be provided to the mysqli_fetch_row function call. I will update my post to include that ...
yes, but instead of mysql_connect, it would be mysqli_connect, whatever variable you store that connection to, is what gets passed as the $link variable I specified in my mysqli_fetch_row function call.
@SlyRaskal mysqli_fetch_row expects 1 parameter only and not 2. It expects the mysqli result only.
@AliTrixx was riht, mysqli_fetch_row has only one parameter, but i used assoc(), that works. Tanks for your help, this is the working solution :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.