-3

i'm very new in php mysql programming. i have write program for retrieving my users profile information, but showed me

mysql_fetch_assoc(): supplied argument is not a valid on line 9 and 21

here is my code. but on the other hand it's working on another database. i edited single quotes but it still give the same error message. can you please give me a solution or if you have like this program made by your own it will be more helpful to compare and identify my lacking's.

<?php
include ("config.php")
?>

<?php
 function getUsersData($id)
 {
    $array=array();
    $result = mysql_query("SELECT * FROM 'users' WHERE 'id' = '" . $id );
    while($row = mysql_fetch_assoc($result))
    {
        $array['id']=$row ['id'];
        $array['name']=$row ['name'];
        $array['username']=$row ['username'];
        $array['email']=$row ['email'];
    }
    return $array;
 }
 function getId($username)
 {
    $result = mysql_query("SELECT 'id' FROM 'users' WHERE 'username' = '" . $username . "'");
    while($row = mysql_fetch_assoc($result))
    {
        return $row['id'];
    }
 }
?>
Falko
  • 17,076
  • 13
  • 60
  • 105
  • 1
    You didn't correctly quote the `$id` value, and incorrectly quoted the column name. Add mysql_error(), then read up on PDO / parameter binding. – mario Oct 28 '14 at 08:38
  • First of all, stop using `mysql` functions, since it's deprecated (what means somewhere in the future it will be kicked out of php and your code won't work anymore). Use `mysqli` instead. – Michel Oct 28 '14 at 08:39
  • MySql query is not right here . so the $result not getting the resource id. As a new developer i suggest you to print your query before execute . The line $result = mysql_query("SELECT * FROM 'users' WHERE 'id' = '" . $id ); should be $result = mysql_query("SELECT * FROM `users` WHERE `id` = '" . $id ."'"); – Subhra Sekhar Mukhopadhyay Oct 28 '14 at 08:43

1 Answers1

2

Most likely because the query failed. Identifier (tables/columns) quotes are backticks not single quotes:

`users`
$result = mysql_query("SELECT * FROM 'users' WHERE 'id' = '" . $id ); // fix the quoting here
                                     ^     ^       ^  ^

Either you include them or omit them.

$id = "'" . mysql_real_escape_string($id) . "'";
$result = mysql_query("SELECT * FROM `users` WHERE `id` = $id");

Obligatory Note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
  • 1
  • 1
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • My question Limit is done ! I am trying to update my data and i wrote this code but its not working Code is .. if (isset($_POST['steup'])){ $deposit_id = $_POST['deposit_id']; $deposit_amount= $_POST['deposit_amount']; $deposit_percentage = $_POST['deposit_percentage']; $deposit_paystatus = 1; $sql1 = " UPDATE tbl_deposit SET deposit_amount = '$deposit_amount', deposit_percentage = '$deposit_percentage', deposit_paystatus = '$ deposit_paystatus' WHERE deposit_id = '$deposit_id' "; $result = $db->query($sql1); } where is the problem please help me – Effetty Hassan Mukul Aug 16 '19 at 22:31