Jump to content

[SOLVED] PHP MySQL query error


erdomester

Recommended Posts

Hello,

I have this code:

 

$tablenames= array();   $tn=0;
$search = $newarray[$i];
$sql_s = "SHOW TABLES FROM db WHERE Tables_in_db LIKE '%$search%' AND Tables_in_db LIKE '%k%'";
$result_s = mysql_query($sql_s) or die ("Error in query: $query. ".mysql_error());
  while ($row_s = mysql_fetch_row($result_s))
   {
    $tablenames = implode("", $row_s);
     echo "HAHO: ".$row_s[0]."<br>";
    $tablenames[$tn] = $row_s[0];
    echo $tn." element of tablenames is ".$tablenames[$tn]."<br>";
    $tn++;
   }

 

Why am I getting this result?:

 

HAHO: b240_hkszcs_ind

0 element of tablenames is b

HAHO: kb240_ny_hkszcsp_ind

1 element of tablenames is k

 

Why is it getting only the first letter of the tablenames???

(I've put a "k" to the front of the second tablename to make sure that it is getting the first letter :S )

Link to comment
https://forums.phpfreaks.com/topic/158718-solved-php-mysql-query-error/
Share on other sites

Try including print_r($row_s) inside the while loop. Post what that prints here.

 

HAHO: b240_hkszcs_ind

Array ( [0] => b240_hkszcs_ind ) 0 element of tablenames is b

HAHO: kb240_ny_hkszcsp_ind

Array ( [0] => kb240_ny_hkszcsp_ind ) 1 element of tablenames is k

You are not doing what you think you are doing  ;D

 

$row_s is just a row from your resultset, so it represents a single table.

Making an implode() on it puts all the data of the table in a string, from which you extract the first char(from the first field in the resultset, that being the table name I assume) with $row_s[0]

If all you are trying to do is transfer the data into an array for later use you could do this using something like the following:

 

$tablenames = array();   
$search = $newarray[$i];
$sql_s = "SHOW TABLES FROM db WHERE Tables_in_db LIKE '%$search%' AND Tables_in_db LIKE '%k%'";
$result_s = mysql_query($sql_s) or die ("Error in query: $query. ".mysql_error());
while ($row_s = mysql_fetch_row($result_s)) {
  //collect all the table names into an array for later use   
  $tablenames[] = $row_s[0];
}
   
//now loop through table names
for($i=0;$tablenames[$i];$i++) {
  echo $tablenames[$i] . "<br>";
}
  

 

;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.