Jump to content

Query Problem


designanddev

Recommended Posts

I keep getting this error ----> Warning: mysql_fetch_assoc() expects parameter 1 to be resource,

 

Code below

 

$sql = "SELECT id FROM users WHERE first_name =".$this->first_name;
$result = mysql_query($sql,$database->connection);
while($row = mysql_fetch_assoc($result)){
echo $row['firstname'];
}

Link to comment
Share on other sites

$firstname = $_POST['firstname'];

$sql = "SELECT id FROM users WHERE first_name =" . $firstname . " LIMIT 1";


$result = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_assoc($result);


echo $row['Firstname'];

 

$mysqli is the database connection.

 

Sorry I did it procedure style, I never learnt object style.

Link to comment
Share on other sites

I manage to get the error message for when fields have not been filled out, now i am beginning to write the process of checking to see

if a current user already exists, is my method query being past the post? i am not sure if it is that why it may have the error

 

Kind Regards

 

Heres my function...

 

// Check for an email first name:
public function Reg() {
global $database;
if(empty($this->last_name)){
  $this->errors['one'] = "You forgot to enter your
  last name."; 
}
if(empty($this->first_name)){
  $this->errors['two'] = "You forgot to enter your
  first name.";   
}
elseif(empty($this->errors)){
// Register the user in the database...
$sql = "SELECT id FROM users WHERE first_name =" . $first_name . " LIMIT 1";
$result = mysql_query($sql,$database->connection);
while($row = mysql_fetch_assoc($result)){
echo $row['first_name'];
}
}
}//End of Reg

 

 

Heres the call...

if(isset($_POST['submit'])) {
$new_user = new User();
$new_user->first_name = $_POST['first_name'];
$new_user->last_name = $_POST['last_name'];
if($new_user->reg()){


}
else {
$first_name = "";
$last_name = "";
}
}

Link to comment
Share on other sites

It does matter.

+1

 

In programming, EVERYTHING matters, because computers only do EXACTLY what your code tells them to do.

 

$this->first_name contains a string and must be enclosed in single-quotes within the query statement so that it will be treated as a literal string instead of a mysql keyword or a column/table/database name. If you had some error checking logic in your code to get the mysql error message that is occurring, it would be complaining about an unknown column name that is the value of the entered first_name.

Edited by PFMaBiSmAd
Link to comment
Share on other sites

+1

 

In programming, EVERYTHING matters, because computers only do EXACTLY what your code tells them to do.

 

$this->first_name contains a string and must be enclosed in single-quotes within the query statement so that it will be treated as a literal string instead of a mysql keyword or a column/table/database name. If you had some error checking logic in your code to get the mysql error message that is occurring, it would be complaining about an unknown column name that is the value of the entered first_name.

 

ok could you show me an example, i can not structure this logic

Link to comment
Share on other sites

Sorry ladies and gents i am still having trouble passing the posted form values into my method query, i can select all and it works fine but when i start to use WHERE clauses it goes all wrong here's my code please take a look.

 

Kind Regards

Submit

if(isset($_POST['submit'])) {
$new_user = new User();
$first_name = $new_user->first_name = $_POST['first_name'];
$last_name = $new_user->last_name = $_POST['last_name'];
if($new_user->reg()){

}
else {
$first_name = "";
$last_name = "";
}
}

 

Method

public function Reg() {
global $database;	
if(empty($this->last_name)){
  $this->errors['one'] = "You forgot to enter your
					last name.";		
}
if(empty($this->first_name)){
  $this->errors['two'] = "You forgot to enter your
					first name.";				
}

elseif(empty($this->errors)){
// Register the user in the database...

$sql = "SELECT id FROM users WHERE first_name =".$this->first_name." LIMIT 1";
$result = mysql_query($sql,$database->connection);
while($row = mysql_fetch_assoc($result)){
echo $row['first_name'];
}

}
}//End of Reg

Edited by designanddev
Link to comment
Share on other sites

I fixed the problem i by changing my query syntax to this...

 

$sql = "SELECT * FROM users WHERE first_name ='{$this->first_name}' LIMIT 1";

 

But i am not sure why this works and the way i normally do my variables in queries did not work,

would be nice to hear that explanation.

 

Kind Regards

 

Leo Scott

Link to comment
Share on other sites

Don't start new threads for the same problem. Merging this thread with your previous one...

 

In your previous thread, someone specifically stated you needed single quotes around the value in the query and they also specifically stated why you needed them -

 

$this->first_name contains a string and must be enclosed in single-quotes within the query statement so that it will be treated as a literal string instead of a mysql keyword or a column/table/database name.

Link to comment
Share on other sites

Don't start new threads for the same problem. Merging this thread with your previous one...

 

In your previous thread, someone specifically stated you needed single quotes around the value in the query and they also specifically stated why you needed them -

 

What i dnt get is that i have been using double quoutes for my queries all along, can someone explain why?

 

Kind Regards

Link to comment
Share on other sites

Your use of double-quotes was terminating the string you were building (which was started with the opening double-quote), concatenating a value with that string (the first dot), then concatenating (the second dot) another double-quoted string onto the the end of that.

 

$sql = "SELECT id FROM users WHERE first_name =".$this->first_name." LIMIT 1";

 

This is equivalent to doing -

 

$sql = "SELECT id FROM users WHERE first_name ={$this->first_name} LIMIT 1";

 

 

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.