Xcode connect error - mysql

I have Xcode 4.6.
I want connect mysql database with my app.
So i found this tutorial
http://www.youtube.com/watch?v=ipppykYUzh4#at=104,http://www.youtube.com/watch?v=tvv1KlZ-594
I am was continue Step by Step but i do not know where is my error.
this is my xcode project. Plese look at this and tell me what is wrong.
https://www.dropbox.com/s/4pj1xj4f736l42m/mysql.zip
Thanks for help.
this is php code
<?php
header('Content-type: application/json');
$DB_HostName = '127.0.0.1';
$DB_Name = 'test';
$DB_User = 'root';
$DB_Pass = '';
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error());
mysql_select_db($DB_Name,$con) or die(mysql_error());
$sql = 'SELECT * FROM phpmysql';
$result = mysql_query($sql,$con) or die(mysql_error());
$num = mysql_numrows($result);
mysql_close();
$rows =array();
while ($r = mysql_fetch_assoc($result)){
$rows[] = $r;
}
echo json_encode($rows);
?>
THIS IS MY ERROR
2013-07-30 10:27:22.335 mysql[4095:c07] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:4460
2013-07-30 10:27:22.336 mysql[4095:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
(0x1c91012 0x10cee7e 0x1c90e78 0xb64665 0xc46c4 0x2c88 0xcd8fb 0xcd9cf 0xb61bb 0xc6b4b 0x632dd 0x10e26b0 0x228dfc0 0x228233c 0x228deaf 0x1022bd 0x4ab56 0x4966f 0x49589 0x487e4 0x4861e 0x493d9 0x4c2d2 0xf699c 0x43574 0x4376f 0x43905 0x4c917 0x1096c 0x1194b 0x22cb5 0x23beb 0x15698 0x1becdf9 0x1becad0 0x1c06bf5 0x1c06962 0x1c37bb6 0x1c36f44 0x1c36e1b 0x1117a 0x12ffc 0x243d 0x2365)
libc++abi.dylib: terminate called throwing an exception
(lldb)

Ok, the problem here is that your PHP script returns json_encode($rows) at first, then a var_dump($rows). So, clearly, it's not a JSON that is returned, while your Objective-C code expects a JSON.
Try adding a header('Content-type: application/json'); in the beginning of your file, and remove the var_dump at the end.
EDIT : this is the new PHP script
<?php
header('Content-type: application/json'); // Specify that the result of your script is a JSON
$DB_HostName = '127.0.0.1';
$DB_Name = 'test';
$DB_User = 'root';
$DB_Pass = '';
$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error());
mysql_select_db($DB_Name,$con) or die(mysql_error());
$sql = 'SELECT * FROM phpmysql';
$result = mysql_query($sql,$con) or die(mysql_error());
$num = mysql_numrows($result);
mysql_close();
$rows =array();
while ($r = mysql_fetch_assoc($result)){
$rows[] = $r;
}
echo json_encode($rows);
?>
EDIT 2 : Here is a similar question to yours, see the answer.

Related

I got empty result with MySQL

I have tried to show the whole table data in MySQL. Rows are (User_id, first_name, last_name, and dept). There is no result when I refresh the page.
<?php
$servername = "fdb19.awardspace.net";
$username = "2598428_db";
$password = "password";
$dbname = "2598428_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql="SELECT * From users";
$result = $conn->query($sql);
while($res = mysqli_fetch_array( $result )) {
echo $res['AverageSatisfactionScore'];
}
?>
Your echo is false.
You need to display a valid column name like :
echo $res['id'] or echo $res['dept'].
just try to print the total count of your result set by
echo mysqli_num_rows( $result );
it will print total number of records in the result
Also try to print whole row by
print_r($res);
insted of using
echo $res['AverageSatisfactionScore'];

Warning: mysql_select_db() expects parameter 2 to be resource,

<?php
$servername = "localhost";
$username = "root";
$password = "Rachel";
$db = "hairdressingapointments";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected Sussessfully";
mysql_select_db('Hairdressingapointments', $conn) or die(mysql_error());
$sql = "SELECT `ApointmentDate`, `ApointmentTime` FROM `apointments` WHERE `staff_id`=1 && `quantity`>0";
if(!mysql_query($sql)){
die('Error: ' . mysql_error());
}
echo $sql;
mysql_close();
?>
spent hours trying to figure this out and im guessing its something so simple. getting back the following error:
Warning: mysql_select_db() expects parameter 2 to be resource, object given in C:\wamp2\www\hairdressingapointments\TeresaApointments.php on line 15 which is,
mysql_select_db('Hairdressingapointments', $conn) or die(mysql_error());
You already connected to the database using
mysqli_connect(...);
So, you do not need
mysql_select_db(....);
Also change the query to this
$sql = "SELECT ApointmentDate, ApointmentTime FROM apointments WHERE staff_id=1 AND quantity>0";
If you use SQLWorkbench or SQLYog or some other tool, you can enter your SQL and make sure it is valid before adding it to your script.
Also, make sure the table name is really
apointments
and not
appointments
I got this information from php.net - mysqli_connect

Getting 2 Notice: Undefined Variable errors

The two errors are as below:
Notice: Undefined variable: HawA_Homes in C:\wamp\www\HawA_CIS241\InsertRecord.php on line 48
Notice: Undefined variable: HawA_Homes in C:\wamp\www\HawA_CIS241\InsertRecord.php on line 56
I've checked my names and they appear correct and I am not sure how to proceed now.
Code is as below:
<?php
$hostName = "localhost";
$databaseName = "test";
$userName = "root";
$password = "";
$tableName = "HawA_Homes";
//try to connect report error if cannot
$db = new mysqli($hostName, $userName, $password, $databaseName) or die(" Could not connect:" . mysql_error());
print(" Connection successful to host $hostName <br /> <br />"); //report connection success
//Get data to create a new record
$Address = $_REQUEST["address"];
$DateBuilt = $_REQUEST["dateBuilt"];
$Value = $_REQUEST["value"];
$Size = $_REQUEST["size"];
$Number_of_floors = $_REQUEST["floors"];
$sql = "INSERT INTO $HawA_Homes('Address','DateBuilt','Value','Size','Number_of_floors')VALUES{'$Address','$DateBuilt','$Value','$Size','$Number_of_floors')"; //Create insert query for new record
//try to query dataase / store returned results and report error if not successful
if(!$result =$db->query($sql))
{
//die('There was an error running the query[' .$db->error . ']';
}
print("SQL query $sql successful to database: $HawA_Homes <br /><br />"); //report sql query successful.
?>
You have these notices because the variable $HawA_Homes isn't declared in your code before being used at line 48 and 56. (These are just notices, they are not critical errors, you can avoid displaying them by adding error_reporting(E_ALL & ~E_NOTICE); at the begining of your code, like explained here)
In fact, you used $HawA_Homes instead of $tableName in these lines. Replace them, you won't have notices anymore for these lines.

Perl DBD error FUNCTION dbName.GLOB does not exist

I am starting to write some Perl scripts for some cron jobs that will query the database and send out reminders about upcoming events. I'm quite new to database access in Perl as most of my work thus far has been on the web end using PHP. Anyway, the first query is working fine to generate a temporary output file and then I'm reading back in that output file to loop thru the results querying to find the specific events for the users discovered in the first query.
The problem that I am running into now is getting the following error:
./remind.pl
DBD::mysql::st execute failed: FUNCTION dbName.GLOB does not exist at ./remind.pl line 41.
SQL Error: FUNCTION dbName.GLOB does not exist
This is my Perl code
$host = 'localhost';
$database = 'dbName';
$user = 'user';
$password = 'password';
use POSIX qw(strftime);
use List::MoreUtils qw(uniq);
use Mail::Sendmail;
use DBI;
$dt = strftime("%Y%m%d%H%M%S", localtime(time));
$List30 = "../tmp/queries/30DayUserList.$dt";
open my $UserList30Day, ">> $List30" or die "Can't create tmp file: $!";
$dbh = DBI->connect('dbi:mysql:dbName',$user,$password) or die "Connection error: $DBI::errstr\n";
$sql = "SELECT DISTINCT user FROM shows WHERE initial_date BETWEEN CURDATE() AND CURDATE() + INTERVAL 30 DAY";
$sth = $dbh->prepare($sql);
$sth->execute or die "SQL Error: $DBI::errstr\n";
while (#jeweler = $sth->fetchrow_array()) {
print $UserList30Day "$user[0]\n";
}
close $UserList30Day;
open my $UserIDList, "< $List30" or die "Can't open temp file: $List30";
while ($id = $UserIDList) { # Read in User ID from temp file as $id
# Query for show information for next 30 days
my $sql = "SELECT shows.initial_date, shows.initial_time, shows.hostess_key, hostess.hostess_fname, hostess.hostess_lname, hostess.primary_phone, hostess.address1, hostess.address2, hostess.city, hostess.zipcode, hostess.state
FROM shows, hostess
WHERE shows.user = $id
AND initial_date BETWEEN CURDATE() AND CURDATE() + INTERVAL 30 DAY
AND shows.hostess_key = hostess.hostess_key";
my $sth = $dbh->prepare($sql);
$sth->execute or die "SQL Error: $DBI::errstr\n";
# Iterate thru query results to create output data
while (#row = $sth->fetchrow_array()) {
$content = "Reminder: You have a show for $row[3] $row[4] coming up on $row[0] at $row[1].\n";
$content .= "Location: $row[6] \n";
if ($row[7] != '') {
$content .= " " . $row[7] . "\n";
}
$content .= " $row[8], $row[10] $row[9] \n";
$content .= "Phone: $row[5] \n";
}
%mail = (To => 'email',
From => 'email',
Subject => 'Just another test',
Message => $content
);
# sendmail(%mail) or die $Mail::Sendmail::error;
print %mail;
}
close $UserList30Day;
Thanks in advance for any assistance.
while ($id = $UserIDList) {
should be
while ($id = <$UserIDList>) {
chomp;

MySQL to XML file

I am trying to get MySQL database into an xml file; here is my code:
<?php
header("Content-type: text/xml");
include 'dbc.php';
$query = "SELECT * FROM airports LIMIT 50";
$result = mysql_query($query, $link)
or die('Error querying database.');
$xml = new SimpleXMLElement('<xml/>');
while($row = mysql_fetch_assoc($result)) {
$draw = $xml->addChild('draw');
$draw->addChild('ident',htmlentities(iconv("UTF-8", "ISO-8859-1//IGNORE",$row['ident'])));
$draw->addChild('name',htmlentities(iconv("UTF-8", "ISO-8859-1//IGNORE",$row['name'])));
}
mysql_close($link);
$fp = fopen("links2.xml","wb");
fwrite($fp,$xml->asXML());
fclose($fp);
Here is the error Im getting:
XML Parsing Error: no element found
Location: /sql2xml2.php
Line Number 1, Column 2:
-^
What am I doing wrong???
Your XML is considered invalid in your XML reader because of the thrown warning, thus the XML Parsing Error: junk after document element issue.
As for the warning itself, you need to escape special entities (namely &, < and > in your content when adding it like that (using str_replace usually works well for only those 3 when it comes to XML, htmlentities may yield undesired effects, unless you supply PHP 5.4's ENT_XML1 mode).
Refer to a related answer for more information of why this happens.
If you want just to export MySQL database to local XML file you can use mysqldump tool:
mysqldump --xml -u username -p databasename [tablename] > filename.xml
Got it to work with this code:
<?
header("content-type:text/xml");
function getXML($query="SELECT * FROM airports limit 50")
{
include 'dbc.php';
$result = mysql_query($query, $link)
or die('Error querying database.');
$columns="";
echo "<xml>\n";
while($row=mysql_fetch_assoc($result))
{
$columns.="\t<airport>\n";
foreach($row as $key => $value)
{
$value = htmlentities(iconv("UTF-8", "ISO-8859-1//TRANSLIT",$value));
$value = htmlentities(iconv("UTF-8", "ISO-8859-1//IGNORE",$value));
$columns.="\t\t<$key>$value</$key>\n";
}
$columns.="\t</airport>\n";
}
echo $columns;
echo "</xml>\n";
}
getXML();
?>