Jump to content

Php function mysql_query


unlishema.wolf
Go to solution Solved by unlishema.wolf,

Recommended Posts

Well it is the time of the year I refresh myself on php again. I am wanting to make a function to connect to the mysql database, run the query, and return the result. However, I also wish to add in a way to get the mysql error if something messes up (for debugging of course).

 

Here is what I have so far:

<?php
$mysql_test_server="localhost";
$mysql_test_username="username";
$mysql_test_password="password";
$mysql_test_database="database";

function mysql_test_query($mysql_test_query, $mysql_test_close=true) {
	mysql_connect($mysql_test_server,$mysql_test_username,$mysql_test_password);
	mysql_select_db($mysql_test_database);
	$mysql_test_result = mysql_query($mysql_test_query);
	if ($mysql_test_close) {
		mysql_close();
	}
	return $mysql_test_result;
}

$result = mysql_test_query("SELECT * FROM people") or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
	echo "First Name: ".$row['first_name']."</ br>";
	echo "Last Name: ".$row['last_name']."</ br>";
}
?>
Link to comment
Share on other sites

I forgot to mention I have attempted the following without any working results.

<?php
$mysql_test_server="localhost";
$mysql_test_username="username";
$mysql_test_password="password";
$mysql_test_database="database";

function usql_query($mysql_test_query, $mysql_test_close=true) {
	mysql_connect($mysql_test_server,$mysql_test_username,$mysql_test_password) or return false;
	mysql_select_db($mysql_test_database) or return false;
	$mysql_test_result = mysql_query($mysql_test_query) or return false;
	if ($mysql_test_close) {
		mysql_close() or return false;
	}
	return $mysql_test_result;
}

$result = usql_query("SELECT * FROM people") or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
	echo "First Name: ".$row['first_name']."</ br>";
	echo "Last Name: ".$row['last_name']."</ br>";
}
?>

However, after reading up on the php functions page I am going to test the following once I get my localhost back up and running.

<?php
$mysql_test_server="localhost";
$mysql_test_username="username";
$mysql_test_password="password";
$mysql_test_database="database";

function usql_query($mysql_test_query, $mysql_test_close=true) {
	$mysql_test_conected = mysql_connect($mysql_test_server,$mysql_test_username,$mysql_test_password);
	if(!$mysql_test_conected) {
		return false;
	}
	if(!mysql_select_db($mysql_test_database)) {
		return false;
	}
	$mysql_test_result = mysql_query($mysql_test_query);
	if(!$mysql_test_result) {
		return false;
	}
	if ($mysql_test_close) {
		if(!mysql_close()) {
			return false;
		}
	}
	return $mysql_test_result;
}

$result = usql_query("SELECT * FROM people") or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
	echo "First Name: ".$row['first_name']."</ br>";
	echo "Last Name: ".$row['last_name']."</ br>";
}
?>

Sorry for double post. I waited to long and can't edit the first post.

Link to comment
Share on other sites

  • Solution

The following code allows you to call one function and run a mysql query without having to leave a mysql connection open or opening it every time. It will open the connection, select the database, run the query, and then close the connection if you wish so.

 

Notice: If a mysql_error() occurs it will not close the connection.

<?php
$mysql_test_server="localhost";
$mysql_test_username="username";
$mysql_test_password="password";
$mysql_test_database="database";

function mysql_test_query($mysql_test_query, $mysql_test_close=true) {
	global $mysql_test_server,$mysql_test_username,$mysql_test_password,$mysql_test_database;
	$mysql_test_conected = mysql_connect($mysql_test_server,$mysql_test_username,$mysql_test_password);
	if(!$mysql_test_conected) {
		return false;
	}
	if(!mysql_select_db($mysql_test_database)) {
		return false;
	}
	$mysql_test_result = mysql_query($mysql_test_query);
	if(!$mysql_test_result) {
		return false;
	}
	if ($mysql_test_close) {
		if(!mysql_close()) {
			return false;
		}
	}
	return $mysql_test_result;
}

$result = mysql_test_query("SELECT * FROM people") or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
    echo "First Name: ".$row['first_name']."</ br>";
    echo "Last Name: ".$row['last_name']."</ br>";
}
?>

Sorry for the third post just wanted to post the solution for my problem for anybody in the future.

Edited by unlishema.wolf
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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