Page 1 of 1

PHP/mySQL: Ouput showing random numbers in error?

Posted: Sat Feb 26, 2011 10:21 am
by bertles86
Hi folks,

Here are my tables:

Code: Select all

CREATE TABLE `client` (
  `client_id` int(11) NOT NULL AUTO_INCREMENT,
  `second_name` varchar(255) NOT NULL,
  `first_name` varchar(255) NOT NULL,
  `invoice_address_1` varchar(255) NOT NULL,
  `invoice_address_2` varchar(255) NOT NULL,
  `invoice_address_3` varchar(255) NOT NULL,
  `invoice_town_city` varchar(255) NOT NULL,
  `invoice_postcode` varchar(8) DEFAULT NULL,
  `primary_number` varchar(20) NOT NULL,
  `alt_number` varchar(20) NOT NULL,
  `email` varchar(255) NOT NULL,
  `contact_date` date NOT NULL,
  PRIMARY KEY (`client_id`)
)

Code: Select all

CREATE TABLE `job` (
  `job_id` int(11) NOT NULL AUTO_INCREMENT,
  `client_id` int(11) DEFAULT NULL,
  `job_address_1` varchar(255) NOT NULL,
  `job_address_2` varchar(255) NOT NULL,
  `job_address_3` varchar(255) NOT NULL,
  `job_town_city` varchar(255) NOT NULL,
  `job_postcode` varchar(8) DEFAULT NULL,
  `date_started` date NOT NULL,
  `date_finished` date NOT NULL,
  `status_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`job_id`),
  KEY `client_id` (`client_id`),
  KEY `status_id` (`status_id`),
  CONSTRAINT `job_ibfk_1` FOREIGN KEY (`client_id`) REFERENCES `client` (`client
_id`),
  CONSTRAINT `job_ibfk_2` FOREIGN KEY (`status_id`) REFERENCES `status` (`status
_id`)
)

Code: Select all

CREATE TABLE `status` (
  `status_id` int(10) NOT NULL DEFAULT '0',
  `status_type` varchar(50) NOT NULL,
  PRIMARY KEY (`status_id`)
)
A basic query of mine is to output all jobs:

Code: Select all

<?php 

	$page_title = 'View all jobs alphabetically';
	
	echo '<h1>View all jobs alphabetically</h1>';
	
	require_once ('../mysqli_connect.php');
	
	//Create Query
	
	$q = "SELECT job.job_id, job.client_id, job.job_address_1, job.job_address_2, job.job_address_3, job.job_town_city, job.job_postcode, job.date_started, job.date_finished, status.status_type
			FROM job
			INNER JOIN status
			ON job.status_id=status.status_id
			ORDER BY job.job_address_1 ASC";
	
	//Run Query
	
	$r = @mysqli_query ($dbc, $q);
	
	if ($r) {
	
	//If it ran OK, display records
	
	echo  '<table border="1" align="center"
	cellspacing="3" cellpadding="3"
	width="100%">
	
	<tr>
		<td align="left"><b>Job ID</b></td>
		<td align="left"><b>Client ID</b></td>
		<td align="left"><b>Job Address 1</b></td>
		<td align="left"><b>Job Address 2</b></td>
		<td align="left"><b>Job Address 3</b></td>
		<td align="left"><b>Job Town/City</b></td>
		<td align="left"><b>Job Postcode</b></td>
		<td align="left"><b>Date Start</b></td>
		<td align="left"><b>Date End</b></td>
		<td align="left"><b>Status</b></td>
	</tr>
	';
	
	//Fetch and print the records
	
	while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC)) {
	echo '<tr>
	<td align="left" width="5%">' . $row['job_id'] . '</td>
	<td align="left" width="5%">' . $row['client_id'] . '</td>12
	<td align="left" width="14%">' . $row['job_address_1'] . '</td>
	<td align="left" width="12%">' . $row['job_address_2'] . '</td>
	<td align="left" width="12%">' . $row['job_address_3'] . '</td>
	<td align="left" width="12%">' . $row['job_town_city'] . '</td>
	<td align="left" width="8%">' . $row['job_postcode'] . '</td>
	<td align="left" width="9%">' . $row['date_started'] . '</td>
	<td align="left" width="9%">' . $row['date_finished'] . '</td>
	<td align="left" width="15%">' . $row['status_type'] . '</td>
	</tr>';
	
	}
	
	echo '</table>'; //Close table
	
	mysqli_free_result ($r); //Free resources
	
	} else {
	
	echo '<p class="error">The data could not be found, sorry.</p>';
	
	echo '<p>' . mysqli_error($dbc) . '<br/><br />Query: ' . $q . '</p>';
	
	}
	
	
	mysqli_close($dbc); //Close database
	
?>
When I load this page, I get all my records fine, but in between my header and the output table the following is output:

Code: Select all

12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12
Does anyone have any ideas on why this is happening? My SQL query is fine, I've tested it on the prompt and it works fine, the PHP seems okay too.

Re: PHP/mySQL: Ouput showing random numbers in error?

Posted: Sat Feb 26, 2011 10:52 am
by DigitalMind
I think the problem is in ../mysqli_connect.php. Show that file.

Re: PHP/mySQL: Ouput showing random numbers in error?

Posted: Sat Feb 26, 2011 10:55 am
by bertles86

Code: Select all

<?php # Database Connection
// Store in the directory above web dir.
//This file contains the database access info.
//This file also establishes a connection to MySQL.

DEFINE ('DB_USER', 'XXX');
DEFINE ('DB_PASSWORD', 'XXX');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'inspired');

//Make the connection:

$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die
('Could not connect to MySQL: ' .
mysqli_connect_error() );

?>

Re: PHP/mySQL: Ouput showing random numbers in error?

Posted: Sat Feb 26, 2011 11:52 am
by DigitalMind
Sorry, I misunderstood your description.
Have a look at

Code: Select all

<td align="left" width="5%">' . $row['client_id'] . '</td>12

Re: PHP/mySQL: Ouput showing random numbers in error?

Posted: Sat Feb 26, 2011 11:58 am
by bertles86
Fantastic thanks!