erdomester Posted May 19, 2009 Share Posted May 19, 2009 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 More sharing options...
Masna Posted May 19, 2009 Share Posted May 19, 2009 Try including print_r($row_s) inside the while loop. Post what that prints here. Link to comment https://forums.phpfreaks.com/topic/158718-solved-php-mysql-query-error/#findComment-837068 Share on other sites More sharing options...
erdomester Posted May 19, 2009 Author Share Posted May 19, 2009 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 Link to comment https://forums.phpfreaks.com/topic/158718-solved-php-mysql-query-error/#findComment-837072 Share on other sites More sharing options...
luca200 Posted May 19, 2009 Share Posted May 19, 2009 You are not doing what you think you are doing $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] Link to comment https://forums.phpfreaks.com/topic/158718-solved-php-mysql-query-error/#findComment-837073 Share on other sites More sharing options...
matto Posted May 19, 2009 Share Posted May 19, 2009 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>"; } Link to comment https://forums.phpfreaks.com/topic/158718-solved-php-mysql-query-error/#findComment-837100 Share on other sites More sharing options...
erdomester Posted May 19, 2009 Author Share Posted May 19, 2009 Thanks. I removed the implode line. I had a similar query in another file connected to this where removing the implode resulted in an error. That is why I didn't give a try here. Link to comment https://forums.phpfreaks.com/topic/158718-solved-php-mysql-query-error/#findComment-837128 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.