0

I've some MySQL queries, but one of them isn't working ("Number 1"). The other query ("Number 2") works fine. But I must repeat "Number 2" many times, more than 100. It's hard. I don't get query "Number 1" to work. What's wrong?

"Number 1"

<?php
require_once ("../config/config.db.inc.php");

$sdd_db_host = DB_HOSTNAME;
$sdd_db_name = DB_DATABASE;
$sdd_db_user = DB_USERNAME;
$sdd_db_pass = DB_PASSWORD;
mysql_connect($sdd_db_host,$sdd_db_user,$sdd_db_pass);
mysql_select_db($sdd_db_name);

mysql_query ("set_client='utf8'");
mysql_query ("set character_set_results='utf8'");
mysql_query ("set collation_connection='utf8_general_ci'");
mysql_query ("SET NAMES utf8");
$menu = mysql_fetch_array(mysql_query("SELECT * `tbl_customers`"));

echo $menu['finskaya_razdel']; ?>

Nothing happend, but it should! ['finskaya_razdel'] isn't only one...

"Number 2"

<?php
require_once ("../config/config.db.inc.php");

$sdd_db_host = DB_HOSTNAME;
$sdd_db_name = DB_DATABASE;
$sdd_db_user = DB_USERNAME;
$sdd_db_pass = DB_PASSWORD;
mysql_connect($sdd_db_host,$sdd_db_user,$sdd_db_pass);
mysql_select_db($sdd_db_name);

mysql_query ("set_client='utf8'");
mysql_query ("set character_set_results='utf8'");
mysql_query ("set collation_connection='utf8_general_ci'");
mysql_query ("SET NAMES utf8");

$menu = mysql_query("SELECT finskaya_razdel FROM `tbl_customers` WHERE customers_id='1008' or customers_id='8'");
$menu_res = mysql_result($menu, 0);

echo $menu_res; ?>

This query works. But it's crazy... I've more than 100 queries to do. Number 1 more "gorgeous". What am I doing wrong?

4
  • 4
    use mysqli or pdo. mysql is deprecated. Commented Oct 20, 2017 at 7:12
  • It would also be interesting what print_r($menu); would return Commented Oct 20, 2017 at 7:19
  • @BarclickFloresVelasquez... thank you... Commented Oct 20, 2017 at 7:30
  • @chade_... Thank you... but print_r even hang a page)) Commented Oct 20, 2017 at 7:31

1 Answer 1

1
  1. msql_fetch_* extracts one row at a time. You have to loop and apply it every time.
  2. To use column names you have to use mysql_fetch_assoc (with mysql_fetch_array you'd have to use index numbers).

So...

$menu = mysql_query("SELECT * FROM tbl_customers");

while ($row = mysql_fetch_assoc($menu))
    echo $row["finskaya_razdel"];

... or if you need to have the results in a php multidimensional array...

$menu = mysql_query("SELECT * FROM tbl_customers");

$results = array();
while ($row = mysql_fetch_assoc($menu))
    $results[] = $row;

NOTE: @BarclickFloresVelasquez is right. You should move to mysqli or PDO

Sign up to request clarification or add additional context in comments.

8 Comments

The FROM was missing in the query. Try now
And i don't need it in "while".. it' must be in different parts of some page)... Yeah i found FROM.. and it's happend; But could i do it without "while".
Then use the second option. Load the results in a php multidimensional array and use it where you want
WOW.. i even don't understand you... Please can you give me an example.
It's already in the answer. The second example. $results would be your array
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.