1

So I've been learning for about 3 months now and am currently using very old procedural techniques and the deprecated mysql extension in my code. So time to take a step forward, ditch my procedural ways and get into object oriented / prepared statements...

This is very basic but I guess everyone has to learn some day. I'm trying to get retrieve and simple dataset from mysql database..

so far I have my connection:

$useri = new mysqli('localhost', 'useri', 'xxx','yyy');
if(mysqli_connect_errno()){
    echo mysqli_connect_error();
        }

I get no errors so I assume this works, and I have my query:

$test_query = "SELECT * FROM t";
$test_query = $useri->real_escape_string($test_query);
 echo $test_query;
  if($result = $useri->query($test_query)){
   while($row = $useri->fetch_object($result)){
    echo $row->id;
 }
 $result->close();
}
$useri->close();

However I get no results... so, 2 questions:

a. what am I doing wrong? and

b. anyone recommend any good tutorials apart from the php manual for this stuff?

Thanks :)

4
  • 1
    Check w3schools. Its good and given brief idea. Commented Mar 4, 2013 at 12:14
  • You need a space between the * and the word FROM in your select query Commented Mar 4, 2013 at 12:15
  • that was just a formatting error when transferring my code.. Commented Mar 4, 2013 at 12:17
  • try this: while ($row = $result->fetch_object()) { insted of this: while($row = $useri->fetch_object($result)){ Commented Mar 4, 2013 at 12:18

2 Answers 2

2

This works for one of the table i have in my db:

$useri = new mysqli('localhost', 'useri', 'xxx','yyy');
if(mysqli_connect_errno()){
echo mysqli_connect_error();
    }

$test_query = "SELECT * FROM t";
$test_query = $useri->real_escape_string($test_query);
  if($result = $useri->query($test_query)){
    while ($row = $result->fetch_object()) { //only this is changed
    echo $row->id;
  }
 $result->close();
 }else{ //check for error if query was wrong
 echo $useri->error;
 }
 $useri->close();   
Sign up to request clarification or add additional context in comments.

4 Comments

this code works if you query right table on right database,and if your table contain id row. Try printing error if $results is not fetched with $useri->error;
the error shows that it is the escaping interfering with the query. If I remove the real_escape_string it works fine, and I suppose it doesn't actually need escaping if I'm going to use a prepare statement... Thank you for your time, much appreciated :)
Why is it so repetitive? I don't see any simplicity in it.
because simplicity was not my goal, i explained what line is changed. just want to put whole code for context.
1
  1. make sure that you have a space after *

    $test_query = "SELECT * FROM t";
    
  2. check this tutorial

    http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/ http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

1 Comment

1 - typo transferring the code. 2. Thanks for those, will give them a read through :)

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.