why i cant connect to mysqli?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

why i cant connect to mysqli?

Post by mekha »

i have this connection:

Code: Select all

<?php
DEFINE('DATABASE_USER', 'xxxxxxxx');
DEFINE('DATABASE_PASSWORD', 'xxxxxxxx');
DEFINE('DATABASE_HOST', 'localhost');
DEFINE('DATABASE_NAME', 'xxxxxxx');
$mysqli = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME);
if (mysqli_connect_errno()) {
	printf("Connect failed: %s\n", mysqli_connect_error());
	exit();
}
	else {
	//connected	
}
?>
...and site_queries.php:

Code: Select all

<?php
	function getfrompages()
	{
		$sqlStr = "select * from tbl_pages where page_id = ?";
		return $sqlStr;
	}
?>
... and site.php:

Code: Select all

<?php
include "connection.php";
include "site_queries.php";
global $mysqli;
?>
<?php
$sql = getfrompages();
if ($result = $mysqli->prepare($sql)) 
{
$rekza = 1;
$result->bind_param("i",$rekza);
$result->execute();	
$result->store_result();
$rowsZ = $result->num_rows;
}
if($rowsZ>0)
{
$row = fetch($result);
}
echo $row["page_title"];
?>
and all the files in the same folder....why my code doesnt work ? the echo ?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: why i cant connect to mysqli?

Post by Christopher »

Try checking $mysqli->connect_errno istead of mysqli_connect_errno().
(#10850)
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: why i cant connect to mysqli?

Post by mekha »

nope:S without results :S
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: why i cant connect to mysqli?

Post by social_experiment »

A sample from the PHP Manual that might be useful to you :)

Code: Select all

<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

/*
 * This is the "official" OO way to do it,
 * BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
 */
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

/*
 * Use this instead of $connect_error if you need to ensure
 * compatibility with PHP versions prior to 5.2.9 and 5.3.0.
 */
if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

echo 'Success... ' . $mysqli->host_info . "\n";

$mysqli->close();
?> 
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: why i cant connect to mysqli?

Post by mekha »

still not working :\
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: why i cant connect to mysqli?

Post by social_experiment »

are you receiving any error messages?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: why i cant connect to mysqli?

Post by mekha »

no :S
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: why i cant connect to mysqli?

Post by social_experiment »

:dubious: Even if you use the snippet of code from the php manual?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: why i cant connect to mysqli?

Post by mekha »

i dont know whats going on!...

the $sql is undefined...the query....even that i included all the files :\
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: why i cant connect to mysqli?

Post by Christopher »

mekha wrote:the $sql is undefined...the query....even that i included all the files :\
What does getfrompages() return?
(#10850)
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: why i cant connect to mysqli?

Post by mekha »

i solved some!...

the problem is here:

if($rowsZ>0)
{
$row = fetch($result);
$title = $row[0]["page_title"];
}

echo $title;

if i echo $rowsZ i get 1 ...thats mean there is results...but the echo dont work!
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: why i cant connect to mysqli?

Post by mekha »

ok thanks...i succeed :D thnk you every one
Post Reply