0

Consider the following code:

<?php

$db_error='Establishing a Database Connection Error';
$localhost='localhost';
$db_user='root';
$db_name='uploader_db';
$db_pass='';
$db_connect=mysql_connect($localhost,$db_user,$db_pass) or die($db_error);
  mysql_select_db($db_name) or die($db_error);
 $result="SELECT 'user','pass','name','email' FROM 'config' ORDER BY 'name'";
 $sql_result=mysql_fetch_array($result,MYSQL_BOTH);

 echo $sql_count=count($sql_result['name']);
?>

I am receiving the following error:

error : Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\xampp\htdocs\Project1\config.php on line 20 0

How can I resolve this issue?

Dharman
  • 30,962
  • 25
  • 85
  • 135
milad
  • 7
  • 1
  • 6
  • Possible duplicate of [mysql\_fetch\_array()/mysql\_fetch\_assoc()/mysql\_fetch\_row()/mysql\_num\_rows etc... expects parameter 1 to be resource](https://stackoverflow.com/questions/2973202/mysql-fetch-array-mysql-fetch-assoc-mysql-fetch-row-mysql-num-rows-etc) – DarkSuniuM Oct 06 '18 at 18:07
  • 1
    FYI, mysql api is deprecated since version 5.5 and it is removed in version 7. Instead use mysqli_ functions or PDO. http://php.net/manual/en/intro.mysql.php – Juan Oct 06 '18 at 18:19

1 Answers1

0

You have not assigned three things properly in the code

1) mysql_select_db($db_name) : Here you need to pass $db_connect

2) Query is not executed : $result=mysql_query($sql_fetch) or die("Error: ".mysql_error());

3)query result needs to be passed to mysql_fetch_array()

Below is the updated code with above changes

$db_error='Establishing a Database Connection Error';

$localhost='localhost';

$db_user='root';

$db_name='uploader_db';

$db_pass='';

$db_connect=mysql_connect($localhost,$db_user,$db_pass) or die($db_error);

mysql_select_db($db_name,$db_connect) or die($db_error);

$sql_fetch="SELECT 'user','pass','name','email' FROM 'config' ORDER BY 'name'";

 $result=mysql_query($sql_fetch) or die("Error: ".mysql_error());

 $sql_result=mysql_fetch_array($result);

 echo $sql_count=count($sql_result['name']);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Vishal
  • 26
  • 2