PDA

View Full Version : PHP help


Falcon Capt
07-04-2015, 07:11 PM
I converted some MySQL code from MySQL to MySQLi and got it working (sort of), just need a little help with the PHP now...

Original code:

if ($_REQUEST['show'] == 'mysqlstats' || empty($_REQUEST['show'])) {
print_form_header('', '');
print_table_header('MySQL Stats &nbsp;<span class="normal">mysql_stat()</span>');
print_description_row(si_mysqlstats());
print_table_footer();
}


function si_mysqlstats() {
$s = '';
$status = explode(' ', mysql_stat());
while ( list($k, $v) = each($status) ) {
$s .= $v . '<br />';
}
return $s;
}

====================

Code that is now working:

if ($_REQUEST['show'] == 'mysqlistats' || empty($_REQUEST['show'])) {
print_form_header('', '');
print_table_header('MySQLi Stats &nbsp;<span class="normal">mysqli_stat()</span>');
print_description_row(si_mysqlistats());
print_table_footer();
}


function si_mysqlistats() {
$link = mysqli_connect("localhost", "dbusername", "dbpassword", "dbname");

printf(mysqli_stat($link));

mysqli_close($link);

return $s;
}

But it doesn't display correctly, see attached image, it is printing above the box instead of in the box.


Also, I would like the display to be:

Uptime: 49602
Threads: 4
Questions: 850693
Slow queries: 16
Opens: 3099
Flush tables: 1
Open tables: 747
Queries per second avg: 17.150


Instead of the current:

Uptime: 49602 Threads: 4 Questions: 850693 Slow queries: 16 Opens: 3099 Flush tables: 1 Open tables: 747 Queries per second avg: 17.150


Any help would be GREATLY appreciated!

kh99
07-04-2015, 07:31 PM
Why did you change the si_mysqlstats to print out the info? You probably just needed to copy the function and change mysql_stat to mysqli_stat.

Falcon Capt
07-04-2015, 07:49 PM
Why did you change the si_mysqlstats to print out the info? You probably just needed to copy the function and change mysql_stat to mysqli_stat.It wouldn't display any of the information just simply changing the function to MySQLi, with the above code was the only way I could get the info to display...

kh99
07-04-2015, 07:55 PM
Oh, right, you needed the $link to call the function. So maybe this:
function si_mysqlistats() {
$link = mysqli_connect("localhost", "dbusername", "dbpassword", "dbname");
$status = explode(' ', mysqli_stat($link));
mysqli_close($link);

$s = '';
while ( list($k, $v) = each($status) ) {
$s .= $v . '<br />';
}
return $s;
}


And if you already have an open connection somewhere in the program, it would probably be better to change the function to take it as a parameter instead of opening another connection.

Falcon Capt
07-04-2015, 08:12 PM
Oh, right, you needed the $link to call the function. So maybe this:
function si_mysqlistats() {
$link = mysqli_connect("localhost", "dbusername", "dbpassword", "dbname");
$status = explode(' ', mysqli_stat($link));
mysqli_close($link);

$s = '';
while ( list($k, $v) = each($status) ) {
$s .= $v . '<br />';
}
return $s;
}


And if you already have an open connection somewhere in the program, it would probably be better to change the function to take it as a parameter instead of opening another connection.

You're awesome! That did it!

Many thanks!

If I already had an open connection elsewhere, what would this code look like?

kh99
07-04-2015, 08:34 PM
You're awesome! That did it!

Many thanks!

If I already had an open connection elsewhere, what would this code look like?

I guess something like:
function si_mysqlistats($link) {
$status = explode(' ', mysqli_stat($link));

$s = '';
while ( list($k, $v) = each($status) ) {
$s .= $v . '<br />';
}
return $s;
}

and then in the other code:
print_description_row(si_mysqlstats($link));


but after I posted that I realized that it looks like you're working on code from the admincp or modcp, so you might just be able to use global $db in place of $link, like

function si_mysqlistats() {
global $db;
$status = explode(' ', mysqli_stat($db));

$s = '';
while ( list($k, $v) = each($status) ) {
$s .= $v . '<br />';
}
return $s;
}


and you wouldn't have to change the other code.