Jump to content

Another MYSQL to MYSQLI issue


V

Recommended Posts

I'm follwing a tutorial that uses MYSQL but I have everything set up using MYSQLI , object-oriented. I don't know how to make the code work.

 

The code is this. Half is object oriented and the rest I can't figure out how to change

 

 

$connection = dbConnect(); //connects to DB

//$post_id value comes from the POSTS table
$post_id = $_GET['post'];

// prepare the SQL query
$sql = "SELECT * FROM comments WHERE post_id='$post_id' ORDER BY com_id DESC LIMIT 9";

$result = $connection->query($sql) or die(mysqli_error($connection));

$timeline='';

while ($row = $result->fetch_assoc()) {	

	$timeline.=formatTweet($row['com_dis'],$row['date']);
}

// fetch the latest tweet
$lastTweet = '';

list($lastTweet) = mysqli_fetch_array(mysqli_query("SELECT com_dis FROM comments ORDER BY com_id DESC LIMIT 1"));

if(!$lastTweet) $lastTweet = "You don't have any tweets yet!";

 

 

I get errors on this line

 

list($lastTweet) = mysqli_fetch_array(mysqli_query("SELECT com_dis FROM comments ORDER BY com_id DESC LIMIT 1"));

 

Warning: mysqli_query() expects at least 2 parameters, 1 given...

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in...

 

I tried a bunch of variation but get parse errors. Can someone please help?

 

Link to comment
Share on other sites

$connection = dbConnect(); //connects to DB <-- I'm assuimg it returns MySQLi object

//$post_id value comes from the POSTS table
$post_id = (int)$_GET['post'];   // actually seems like coming from GET table
//notice I'm casting it to int (integer) as a way of protection against SQL injection


// prepare the SQL query
$sql = "SELECT * FROM comments WHERE post_id=$post_id ORDER BY com_id DESC LIMIT 9";

$result = $connection->query($sql) or die($connection->error);

$timeline='';

while ($row = $result->fetch_assoc()) {
  $timeline.=formatTweet($row['com_dis'],$row['date']);
}

// fetch the latest tweet
$sql= "SELECT com_dis FROM comments ORDER BY com_id DESC LIMIT 1";
$result = $connection->query($sql) or die($connection->error);

if($row = $result->fetch_assoc()) {
  $lastTweet = $row['com_dis'];
} else {
  $lastTweet = "You don't have any tweets yet!";
}

Link to comment
Share on other sites

Yup I use real_escape_string() everywhere too but I'm sure parts of my script are still vulnerable to attacks. I'm planning to learn in-depth about sql prevention once everything is functional. :) 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.