Cześć, piszę sobie taki mały programik, który ma wysyłać jakiś tekst do bazy danych i go też pobierać. Działa to na ajaxie. Ajax działa i wysyła 'zapytania' do pliku php, który działa na xamppie. Problem polega na tym, że w momencie łączenia się z bazą w 'new mysqli(...);' program się zatrzymuje czy kij wie co i nic nie wraca. Bez tego, php normalnie zwraca wszystkie informacje - tzn. jak wpisze 'echo "COŚ";' to w konsoli zwróci mi, że ajax dostał "COŚ". Tutaj kod dwóch metod na wysyłanie i odbieranie danych plus php:
var _loadData = () => { if(_isConnected) { $$.post( _host, { responseFor: "load", channel: _channel }, (data, xhr, status) => { console.log(data); console.log(xhr); console.log(status); }, (xhr, status) => { console.log(xhr); console.log(status); _initState.alert("Cannot to load data."); } ); } else { _initState.alert("No internet connection."); } }; var _sendData = (dataStr) => { _messages.push({ info: dataStr }); $$.post( _host, { responseFor: "send", info: dataStr, channel: _channel }, (data, xhr, status) => { console.log(data); }, (xhr, status) => { _initState.alert("Cannot to send data."); } ); _updateMessagesList(); };
php:
<?php class DatabaseManager{ private $link; private $debbuggingMode; public function __construct($host, $user, $password, $database) { $this->link = new mysqli($host, $user, $password, $database); if(!$this->link) { $this->err('Could not connect: ' . mysqli_error($this->link)); } } public function setDebbuggingMode($dbgMode) { $this->debbuggingMode = $dbgMode; } private function err($info) { if($this->debbuggingMode) { echo $info; } if($this->link) { $this->close(); } exit(); } public function load($channel) { echo "DANE:"; $query = "SELECT 'info', 'channel' FROM 'messages' WHERE 'channel'='"+$channel+"' ORDER BY LIMIT 15"; if($result = mysqli_query($this->link, $query)) { while($row = $result->fetch_row()) { echo $row[0]."".$row[1]."\n"; } $result->close(); } else { echo $this->err('Query failed: ' . mysqli_error($this->link)); } } public function send($info, $channel) { echo "WYSYŁANIE:"; $query = "INSERT INTO 'messages' ('id', 'channel', 'info') VALUES (NULL, '"+$channel+"', '"+$channel+"')"; if(mysqli_query($this->link, $query)) { echo "SUCCESS"; } else { echo $this->err('Query failed: ' . mysqli_error($this->link)); } } public function close() { mysqli_close($this->link); } } if(isset($_POST['responseFor']) && isset($_POST['channel'])) { $dbManager = new DatabaseManager('127.0.0.1:8080', 'root', '', 'cserver'); $dbManager->setDebbuggingMode(TRUE); if($_POST['responseFor'] == "load") { if(isset($_POST['channel'])) { echo "BDF"; $dbManager->load($_POST['channel']); } else { echo "NO_CHANNEL"; } } else if($_POST['responseFor'] == "send") { if(isset($_POST['info'])) { if(isset($_POST['channel'])) { echo "BDF"; $dbManager->send($_POST['info'], $_POST['channel']); } else { echo "NO_CHANNEL"; } } else { echo "NO_INFO"; } } else { echo "NO_QUERY_FOR"; } $dbManager->close(); } else { echo "NO_QUERY_FOR"; } ?>
Co ja tu zwaliłem?